<?php

namespace App\Http\Controllers;

use App\Http\Requests\FsUserImRequest;
use App\Models\FsUserHasIm;
use App\Models\FsUserIm;
use App\Transformers\FsUserImTransformer;
use Illuminate\Http\Request;



class FsUserImController extends Controller
{
    /**
     * @api {post} /api/fsUser/im  用户微信绑定会员微信
     * @apiGroup FsUserIm
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {array} ids  会员Id
     * @apiParam {int} im_id  im的id
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 201 OK
     *       {
     *           "message": "添加成功",
     *           "status_code": 200
     *       }
     */
    public function store(FsUserImRequest $request,FsUserIm $fsUserIm,FsUserHasIm $fsUserHasIm){
        foreach($request->ids as $v){
            $res = $fsUserIm->where([
                ['im_id','=',$request->im_id],
                ['fs_user_id','=',$v]
            ])->first();
            if(!$res){
                $fsUserIm->create([
                    'im_id' => $request->im_id,
                    'fs_user_id' => $v,
                ]);
                $fsUserHasIm->where('fs_user_id',$v)->increment('add_im_count');
            }
        }
        return response()->json(['message'=>'添加成功','status_code'=>200], 200);
    }

    /**
     * @api {patch} /api/fsUser/im 用户微信绑定会员微信修改
     * @apiGroup FsUserIm
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {array} ids  会员Id
     * @apiParam {int} im_id  im的id
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 201 OK
     *       {
     *           "message": "添加成功",
     *           "status_code": 200
     *       }
     */
    public function update(FsUserImRequest $request,FsUserIm $fsUserIm,FsUserHasIm $fsUserHasIm){
        $res = $fsUserIm->where('im_id',$request->im_id)->pluck('ms_id')->toArray();
        dd($res);
        if($res){
            $fsUserHasIm->whereIn('id',$res)->decrement('add_im_count');
        }

        $fsUserIm->where('im_id',$request->im_id)->delete();
        foreach($request->ids as $v){
            $fsUserIm->create([
                'im_id' => $request->im_id,
                'fs_user_id' => $v,
                'add_im_count' => count($request->ids)
            ]);
            $fsUserHasIm->where('fs_user_id',$v)->increment('add_im_count');
        }
        return response()->json(['message'=>'修改成功','status_code'=>200], 200);
    }

    /**
     * @api {get} /api/fsUser/im?include=fsUser,im 会员IM列表
     * @apiGroup FsUserPhoneRemark
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     * @apiParam {int} id im的id
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *       {
     *           "data": [
     *               {
     *                   "id": 1,
     *                   "status": "用户正忙",
     *                   "user": "admin",
     *                   "created_at": "2019-01-02 16:43:03",
     *                   "updated_at": "2019-01-02 16:43:03"
     *               }
     *           ]
     *       }
     */
    public function index(Request $request, FsUserIm $fsUserIm){
        $data = $request->validate([
            'id' => 'required|exists:user_ims',
        ]);
        $data = $fsUserIm->where('im_id',$data['id'])->orderBy('created_at','DESC')->get();
        return $this->response->collection($data, new FsUserImTransformer());
    }
}
